<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible" class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
					<el-col :span="24">
						<el-form-item label="供应商编码" prop="code">
							<el-input v-model='dataForm.code' placeholder='请输入供应商编码,限制100字' clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="供应商名称" prop="name">
							<el-input v-model='dataForm.name' placeholder='请输入供应商名称,限制100字' clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="供应商类型" prop="type">
							<el-select v-model='dataForm.type' placeholder='请选择供应商类型' clearable required :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in typeOptions" :key="index" :label="item.fullName" :value="item.enCode" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="供应商分类" prop="classify">
							<el-select v-model='dataForm.classify' placeholder='请选择供应商分类' clearable required :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in classifyOptions" :key="index" :label="item.fullName" :value="item.enCode" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备注" prop="remarks">
							<el-input v-model='dataForm.remarks' placeholder='请输入备注' show-word-limit :style='{ "width": "100%" }'
								type='textarea' :autosize='{ "minRows": 4, "maxRows": 4 }' maxlength='2000'>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="有效标志" prop="enabledMark">
							<el-switch v-model='dataForm.enabledMark' required :active-value='1' :inactive-value='0' >
							</el-switch>
						</el-form-item>
					</el-col>
				</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	//import request from '@/utils/request'
	import {GetSupplierInfo,createSupplier,updateSupplier} from '@/api/q1/basedata/supplier'
	export default {
		components: {  },
		props: [],
		data() {
			const validatePass = (rule,value,callback)=>{
				if(!value || value.trim().length == 0){
					callback(new Error("不能为空"));
				}else if(value.trim().length > 100){
					callback(new Error("长度不能超过100字"));
				}else{
					callback();
				}
			};
			return {
				btnLoading:false,
				loading: false,
				visible: false,
				dataForm: {
					id:'',
					code:undefined,
					name:undefined,
					type:undefined,
					classify:undefined,
					remarks:undefined,
					enabledMark: 1,
				},
				rules: {
					code:[
						{
							required:true,
							validator: validatePass,
							trigger:'"blur"',
						},
					],
					name:[
						{
							required:true,
							validator: validatePass,
							trigger:'"blur"'
						},
					],
					type:[
						{
							required:true,
							message:'请选择供应商类型',
							trigger:'"change"'
						},
					],
					classify:[
						{
							required:true,
							message:'请选择供应商分类',
							trigger:'"change"'
						},
					],
				},
				typeOptions: [],
				classifyOptions: [],
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getDictionaryData();
		},
		mounted() {
        },
		methods: {
			getDictionaryData() {
				this.$store.dispatch('base/getDictionaryData', { sort: 'supplierType' }).then((res) => {
					this.typeOptions = res
				})
				this.$store.dispatch('base/getDictionaryData', { sort: 'supplierClassify' }).then((res) => {
					this.classifyOptions = res
				})
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id) {
				this.dataForm.id = id || 0;
                this.visible = true;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						GetSupplierInfo(id).then(res =>{
							this.dataForm = res.data;
						})
					}
				});
				//this.$store.commit('generator/UPDATE_RELATION_DATA', {})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
						this.btnLoading = true;
						const formMethod = !this.dataForm.id ? createSupplier : updateSupplier;
						formMethod(this.dataForm).then((res) => {
							this.btnLoading = false;
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
										this.btnLoading = false;
                                        this.visible = false,
                                        this.$emit('refresh', true)
                                    }
                                })
                            }).catch((res) => {
								this.btnLoading = false;
							})
                    }
                })
			},
		}
	}
</script>
